package com.ton.utils;

import com.ton.config.data.AdminLoginUser;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class AccessUtil {

    public static HttpServletRequest getRequest() {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

        HttpServletRequest request = servletRequestAttributes.getRequest();
        return request;
    }

    public static HttpServletResponse getResponse() {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

        HttpServletResponse response = servletRequestAttributes.getResponse();
        return response;
    }

    public static AdminLoginUser getAdminLoginUser() {
        return (AdminLoginUser) getRequest().getAttribute("admin_login_user");
    }

    public static Integer getAdminLoginUserId() {
        AdminLoginUser user = getAdminLoginUser();
        if (user != null) {
            return user.getUserId();
        }
        return null;
    }

}
